//
//  CarOrderTableViewCell.swift
//  koolcar
//
//  Created by kuaimaxiansheng on 2023/11/1.
//

import UIKit

class CarOrderTableViewCell: UITableViewCell {
    var stateImageView: UIImageView!
    var stateLab: UILabel!
    var timeLab: UILabel!
    var bgView: UIView!
    var logoImageView: UIImageView!
    var typeLab: UILabel!
    var carImageView: UIImageView!
    var specialBtn: UIButton!
    var nameLab: UILabel!
    var idBtn: UIButton!
    var priceLab: UILabel!
    var handselPriceLab: UILabel!//定金
    var model: OrderInfoModel!//model
    var timeCount: NSInteger = 0 //倒计时
    var timer: Timer!
//    var gradient: CAGradientLayer!

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        self.backgroundColor = .clear
        self.clipsToBounds = true
        self.initUI()
        //self.createBgLayer()
    }
    
//    private func createBgLayer(){
//        gradient = CAGradientLayer.init()
//        gradient.frame = CGRect.init(x: 0, y: 0, width: kScreenWidth, height: 124 + IphoneTopArea);//CAGradientLayer的控件大小
//
//        var tempColorArray: [CGColor] = [UIColor.init(hex: 0xFED764, alpha: 1).cgColor, UIColor.init(hex: 0xFEFCF6, alpha: 0.27).cgColor]
//        var locations: [NSNumber] = [0, 1]
//        self.gradient?.colors = tempColorArray //渐变颜色
//        self.gradient?.locations = locations //渐变起始位置
//        self.gradient?.startPoint = CGPoint.init(x: 0, y: 0)
//        self.gradient?.endPoint = CGPoint.init(x: 0, y: 1)
//        self.contentView.layer.insertSublayer(gradient, at: 0)
//    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func initUI(){
        
        self.stateImageView = UIImageView(frame: .zero)
        self.contentView.addSubview(self.stateImageView)
        
        
        self.stateLab = UILabel(frame: .zero)
        self.stateLab.textColor  = Theme.titleLabColor
        self.stateLab.font = UIFont.init(name: Theme.PingFangSCMedium, size: 16)
        self.contentView.addSubview(self.stateLab)
        
        self.timeLab = UILabel(frame: .zero)
        self.timeLab.textColor = Theme.redColor
        self.timeLab.textAlignment = .left
        self.timeLab.font = UIFont.init(name: Theme.PingFangSCRegular, size: 12)
        self.contentView.addSubview(self.timeLab)
        
        self.bgView = UIView(frame: .zero)
        self.bgView.backgroundColor = .white
        self.bgView.layer.cornerRadius = 8
        self.bgView.layer.masksToBounds = true
        self.contentView.addSubview(self.bgView)
        
        self.logoImageView = UIImageView(frame: .zero)
        self.bgView.addSubview(self.logoImageView)
        
        self.typeLab = UILabel(frame: .zero)
        self.typeLab.textColor = Theme.titleLabColor
        self.typeLab.font = UIFont.init(name: Theme.PingFangSCMedium, size: 14)
        self.bgView.addSubview(self.typeLab)
              
        
        
        self.carImageView = UIImageView(frame: .zero)
        self.bgView.addSubview(self.carImageView)
        
        self.specialBtn = UIButton(frame: .zero)
        self.specialBtn.setBackgroundImage(UIImage(named: "icon_special"), for: .normal)
        self.specialBtn.setTitleColor(.white, for: .normal)
        self.specialBtn.titleLabel?.font = UIFont.init(name: Theme.PingFangSCRegular, size: 11)
        self.carImageView.addSubview(self.specialBtn)
        self.specialBtn.setTitle(MMLocalizedString("special", comment: "特价"), for: .normal)
        
        
        
        self.nameLab = UILabel(frame: .zero)
        self.nameLab.textColor = Theme.titleLabColor
        self.nameLab.font = UIFont.init(name: Theme.PingFangSCMedium, size: 14)
        self.nameLab.numberOfLines = 0
        self.bgView.addSubview(self.nameLab)
        
        
        
        self.idBtn = UIButton(frame: .zero)
        self.idBtn.setTitleColor(Theme.titleLabColor, for: .normal)
        self.idBtn.titleLabel?.font = UIFont.init(name: Theme.PingFangSCRegular, size: 11)
        self.idBtn.setImage(UIImage(named: "icon_copy"), for: .normal)
        self.idBtn.semanticContentAttribute = .forceRightToLeft
        self.idBtn.addTarget(self, action: #selector(copyIdBtnAntion), for: .touchUpInside)
        self.bgView.addSubview(self.idBtn)

        
        
        
        self.priceLab = UILabel(frame: .zero)
        self.priceLab.textColor = Theme.titleLabColor
        self.priceLab.font = UIFont.init(name: Theme.PingFangSCRegular, size: 12)
        self.bgView.addSubview(self.priceLab)
        
        
        
        
        self.handselPriceLab = UILabel(frame: .zero)
        self.handselPriceLab.textColor = Theme.titleLabColor
        self.handselPriceLab.font = UIFont.init(name: Theme.PingFangSCMedium, size: 16)
        self.bgView.addSubview(self.handselPriceLab)
        
          
        
        
        self.logoImageView.image = UIImage(named: "icon_order_car")
        self.typeLab.text = MMLocalizedString("used_car", comment: "二手车")
//        self.stateLab.text = "待支付"
        self.carImageView.backgroundColor = Theme.mainColor
//        self.nameLab.text = "Volkswagen 2023 e-Golf 330Volkswagen 2023 e-Golf 330"
//        self.priceLab.text = "HK$1,688,000"
//        self.handselPriceLab.text = "定金 HK$1,688,000"
//        self.idBtn.setTitle("ID：20230715121", for: .normal)
        self.addLayout()
        
    }
    
    func addLayout(){
        
        self.stateImageView.snp.makeConstraints { make in
            make.top.equalTo(10)
            make.left.equalTo(14)
            make.width.height.equalTo(26)
        }
        
        self.stateLab.snp.makeConstraints { make in
            make.top.equalTo(10)
            make.left.equalTo(self.stateImageView.snp.right).offset(4)
            make.height.equalTo(26)
        }
        
        
        self.timeLab.snp.makeConstraints { make in
            make.top.equalTo(10)
            make.left.equalTo(self.stateLab.snp.right).offset(10)
            make.height.equalTo(26)
        }
        
        
        self.bgView.snp.makeConstraints { make in
            make.top.equalTo(self.stateImageView.snp.bottom).offset(10)
            make.left.equalTo(14)
            make.right.equalTo(-14)
            make.height.greaterThanOrEqualTo(197)
            make.bottom.equalTo(0)
        }
        
        self.logoImageView.snp.makeConstraints { make in
            make.top.equalTo(21)
            make.left.equalTo(14)
            make.width.equalTo(19)
            make.height.equalTo(20)
        }
        
        self.typeLab.snp.makeConstraints { make in
            make.top.equalTo(21)
            make.left.equalTo(self.logoImageView.snp.right).offset(4)
            make.height.equalTo(20)
        }
        
        
      
        
        self.carImageView.snp.makeConstraints { make in
            make.top.equalTo(self.logoImageView.snp.bottom).offset(10)
            make.left.equalTo(14)
            make.width.equalTo(90)
            make.height.equalTo(68)
        }
        
        self.specialBtn.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.right.equalTo(0)
            make.width.equalTo(33)
            make.height.equalTo(18)
        }
        
        
        self.nameLab.snp.makeConstraints { make in
            make.top.equalTo(self.logoImageView.snp.bottom).offset(10)
            make.left.equalTo(self.carImageView.snp.right).offset(10)
            make.right.equalTo(-14)
        }
        

        self.idBtn.snp.makeConstraints { make in
            make.top.equalTo(self.nameLab.snp.bottom).offset(6)
            make.left.equalTo(self.carImageView.snp.right).offset(10)
            make.height.equalTo(20)
        }
        
        self.priceLab.snp.makeConstraints { make in
            make.top.equalTo(self.idBtn.snp.bottom).offset(6)
            make.left.equalTo(self.carImageView.snp.right).offset(10)
            make.right.equalTo(-14)
            make.height.equalTo(20)
        }
        
       
        
        self.handselPriceLab.snp.makeConstraints { make in
            make.right.equalTo(-10)
            make.height.equalTo(22)
            make.bottom.equalTo(-20)
        }
    
        
    }
    
    
    //setModel(车辆详情)
    func setModel(model: OrderInfoModel){
        self.model = model
        self.logoImageView.image = UIImage(named: "icon_order_car")
        self.typeLab.text = MMLocalizedString("used_car", comment: "二手车")
        self.carTypeDealData(model: model)
        self.updateConstraints()
    }
    
    
    //setModel（订单商品详情）
    func setStoreModel(model: OrderInfoModel){
        self.model = model
        self.bgView.snp.updateConstraints { make in
            make.top.equalTo(self.stateImageView.snp.bottom).offset(0)
            make.height.greaterThanOrEqualTo(0)
        }
        self.carTypeDealData(model: model)
        self.updateConstraints()
    }
    
    
    
    
    //车辆
    func carTypeDealData(model: OrderInfoModel){
        
        let price = XTManageCenter.showPriceInComma(source: (model.price ?? 0.00).cleanZero)
        self.priceLab.text = XTManageCenter.currentSign() + price
   
        self.timeLab.text = ""
        let depositAmount = XTManageCenter.showPriceInComma(source: (model.depositAmount ?? 0.00).cleanZero)
        switch model.status {
            case 1:
                self.stateImageView.image = UIImage(named: "icon_detail_unpaid")
                self.stateLab.text = MMLocalizedString("unpaid", comment: "待支付")
                self.setOrderDescribtTimeLab()
                self.handselPriceLab.text = String(format: "%@%@ %@", MMLocalizedString("handsel", comment: "订金"), XTManageCenter.currentSign(),depositAmount)
            case 2:
                if model.subscribeType ?? -1 == 0 {
                    //表示订购中
                    self.stateLab.text = MMLocalizedString("reserved_ing", comment: "订购中")
                }else{
                    self.stateLab.text = MMLocalizedString("reserved", comment: "已预订")
                }
                self.stateImageView.image = UIImage(named: "icon_detail_reserved")
                self.handselPriceLab.text = String(format: "%@%@ %@", MMLocalizedString("handsel", comment: "订金"), XTManageCenter.currentSign(),depositAmount)
            case 3:
                self.stateImageView.image = UIImage(named: "icon_detail_finished")
                self.stateLab.text = MMLocalizedString("finished", comment: "已完成")
                self.handselPriceLab.text = String(format: "%@%@ %@", MMLocalizedString("handsel", comment: "订金"),
                                                   XTManageCenter.currentSign(),
                                                   depositAmount)
            case 4:
                self.stateImageView.image = UIImage(named: "icon_detail_unsubscribed")
                self.stateLab.text = MMLocalizedString("unsubscribed", comment: "已退订")
                self.handselPriceLab.text = String(format: "%@%@ %@", MMLocalizedString("handsel", comment: "订金"),
                                                   XTManageCenter.currentSign(),
                                                   depositAmount)
            case 5:
                self.stateImageView.image = UIImage(named: "icon_detail_canceled")
                self.stateLab.text = MMLocalizedString("canceled", comment: "已取消")
                self.handselPriceLab.text = String(format: "%@%@ %@", MMLocalizedString("handsel", comment: "订金"),
                                                   XTManageCenter.currentSign(),
                                                   depositAmount)
            default:
                self.stateLab.text = ""
        }
        
        self.carImageView.sd_setImage(with: URL.init(string: model.productImage ?? ""))
        self.nameLab.text = model.carName ?? ""
        self.idBtn.setTitle( "ID：" + (model.carNo ?? ""), for: .normal)
    }
    
    //支付时间
    func setOrderDescribtTimeLab()  {
        self.timeCount = XTManageCenter.getTimeFromNowSecond(time: self.model?.effectiveTime ?? "")
        if self.timer != nil  {
            self.timer.invalidate()
        }
        if self.timeCount > 0 {
            //顶部提示状态不隐藏
            UIView.animate(withDuration: 0.2, animations: {
            }) { (flag) in
            }
            self.timer = Timer.scheduledTimer(timeInterval: 1, target: self , selector: #selector(setTime), userInfo: nil , repeats: true)
            RunLoop.main.add(self.timer, forMode: RunLoopMode.commonModes)
        }
    }
    
    
    
    
    //MARK: - Antion
    
    @objc func setTime()  {
        if self.timeCount >= 0 {
            self.timeCount = self.timeCount - 1
            let str = XTManageCenter.calculateDateTimeFromNowBySecondaa(ms: NSInteger(self.timeCount))
//            self.timeLab.text = String(format: "00:%@ %@",str,MMLocalizedString("automatically_cancelled", comment: "%@后自动取消"))
            self.timeLab.text = String(format: MMLocalizedString("automatically_cancelled", comment: "00:%@后自动取消"), str)
        }else{
            self.timeLab.text = ""
            self.timer.invalidate()
            //刷新详情
            self.model?.refreshDataBlock?()
            UIView.animate(withDuration: 0.2, animations: {
                
            }) { (flag) in
            }
        }
    }
    
    
    
    //才
    @objc func copyIdBtnAntion(){
        let past = UIPasteboard.general//系统自带的复制
        past.string = self.model?.carNo ?? ""
        self.makeToast(message: MMLocalizedString("copy_successed", comment: "复制成功"))
    }
    
    
}
